<template>
	<view>
		<view class="search-top">
			<uni-search-bar @input="search" radius="30" cancelButton="none" placeholder="搜索"></uni-search-bar>
		</view>
		<!-- 搜索历史 -->
		<view class="history" v-if="kw===''">
			<view class="title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="28"></uni-icons>
			</view>
			<!-- 内容 -->
			<view class="content">
				<uni-tag @click="goList(item)" v-for="item,index in reHistory" :key="index" type="warning"
					inverted="true" :text="item"></uni-tag>
			</view>
		</view>
		<!-- 搜索列表 -->
		<view class="search-list" v-else>
			<view @click="goDetail(item)" class="search-item" v-for="item,index in searchList" :key="index">
				<text>{{item.goods_name}}</text>
				<uni-icons type="right"></uni-icons>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				searchList: [],
				timer: null,
				kw: '',
				history: JSON.parse(uni.getStorageSync('history') || '[]')
			};
		},
		computed: {
			reHistory() {
				return [...this.history].reverse()
			},
		},
		methods: {
			//跳转商品详情页面
			goDetail(item) {
				uni.navigateTo({
					url: '/subpkg/good_detail/good_detail?goods_id=' + item.goods_id
				})
			},
			// 跳转商品列表页面
			goList(item) {
				uni.navigateTo({
					url: '/subpkg/goods-list/goods-list?query=' + item
				})
			},
			//保存搜索关键词为历史记录
			saveHistory() {
				// 1.将 Array 数组转化为Set 对象
				const set = new Set(this.history)
				// 2.调用 Set对象的delete方法,移除对应的元素
				set.delete(this.kw)
				// 3.调用 Set 对象的add方法,向 Set中添加元素
				set.add(this.kw)
				//4.将Set对象转化为 Array数组
				this.history = Array.from(set)
				// 调用 uni.setStorageSync(key,value) 将搜索历史记录持久化存储到本地
				uni.setStorageSync('history', JSON.stringify(this.history))
			},
			search(val) {
				this.kw = val
				clearTimeout(this.timer)
				if (val.trim() === '') {
					//清空data中保存的搜索历史
					this.searchList = []
					return
				}
				this.timer = setTimeout(async () => {
					const {
						data: {
							message,
							meta
						}
					} = await uni.$http.get('/api/public/v1/goods/qsearch', {
						query: val
					})
					if (meta.status !== 200) return uni.$showMsg()
					this.searchList = message
					// console.log(message)
					this.saveHistory()
				}, 3000)
			}
		}
	}
</script>

<style lang="scss">
	.search-top {
		position: sticky;
		top: 0%;
		left: 0%;
	}

	.uni-searchbar {
		background-color: #c00000
	}

	.uni-tag {
		margin: 0 0 6px 6px;
	}

	.history {
		.title {
			height: 48px;
			border-bottom: 1px solid #eee;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 6px;
			font-size: 12px;
		}

		.content {
			padding-top: 6px;
			display: flex;
			flex-wrap: wrap;

			uni-tag {
				margin-bottom: 10px;
			}
		}
	}

	.search-list {
		.search-item {
			height: 40px;
			border-bottom: 1px solid #eee;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 6px;
			font-size: 12px;

			text {
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}
	}
</style>
